package com.wang.lock;

import com.wang.thread.ThreadNode;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
    public static class A {
        public final String name = "asd".substring(1);
    }
    public static class B extends A {

    }
    public static class ALock implements Lock {
        private AQS aqs = new AQS();
        private static class AQS extends AbstractQueuedSynchronizer {
            @Override
            protected boolean tryAcquire(int arg) {
                setExclusiveOwnerThread(Thread.currentThread());
                super.tryAcquire(arg);
                return true;
            }

            @Override
            protected boolean tryRelease(int arg) {
                setExclusiveOwnerThread(null);
                return true;
            }

            @Override
            protected boolean isHeldExclusively() {
                return getExclusiveOwnerThread() == Thread.currentThread();
            }

            Condition newCondition() {
                return new ConditionObject();
            }
        }

        @Override
        public void lock() {
            this.aqs.acquire(1);
        }

        @Override
        public void lockInterruptibly() throws InterruptedException {
            this.aqs.acquireInterruptibly(1);
        }

        @Override
        public boolean tryLock() {
            return this.aqs.tryAcquire(1);
        }

        @Override
        public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
            return this.aqs.tryAcquireNanos(1,unit.toNanos(time));
        }

        @Override
        public void unlock() {
            this.aqs.release(0);
        }

        @NotNull
        @Override
        public Condition newCondition() {
            return this.aqs.newCondition();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println((new B()).name);
//        ALock lock = new ALock();
//        final Condition condition = lock.newCondition();
//        (new Thread(()->{
//            try {
//                System.out.println("123");
//                condition.await();
//                System.out.println("123");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        })).start();
//        Thread.sleep(1000);
//        lock.lock();
//        condition.signal();
//        lock.unlock();
//        System.out.println("123zxc");
//        Thread.sleep(5000);
    }
}
